package com.headline.todaynews;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.headline.todaynews.anim.CustomScaleAnimation;
import com.headline.todaynews.anim.CustomSlideAnimation;
import com.headline.todaynews.anim.CustomZoomAnimation;
import com.headline.todaynews.fragment.FragmentChangeActivity;
import com.headline.todaynews.fragment.ResponsiveUIActivity;
import com.headline.todaynews.model.NewsSummary;
import com.headline.todaynews.model.TestData;
import com.headline.volley.VolleyDataRequester;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends FragmentActivity implements View.OnClickListener{

    public Button attach;
    public Button changing_fragments;
    public Button left_and_right;
    public Button responsive_ui;
    public Button viewpager;
    public Button title_bar_slide;
    public Button title_bar_content;
    public Button anim_zoom;
    public Button anim_scale;
    public Button anim_slide;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        jsonRequestGetHttpsExample();
        stringRequestGetHttpExample();

        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Point.class, new PointAdapter())
                .enableComplexMapKeySerialization()
                .create();

        Map<Point, String> original = new LinkedHashMap<Point, String>();
        original.put(new Point(5, 6), "a");
        original.put(new Point(8, 8), "b");

        String string=gson.toJson(original,  new TypeToken<Map<Point, String>>() {
        }.getType());

        Log.d("MAP",string);

        attach=(Button)findViewById(R.id.attach);
        changing_fragments=(Button)findViewById(R.id.changing_fragments);
        left_and_right=(Button)findViewById(R.id.left_and_right);
        responsive_ui=(Button)findViewById(R.id.responsive_ui);
        viewpager=(Button)findViewById(R.id.viewpager);
        title_bar_slide=(Button)findViewById(R.id.title_bar_slide);
        title_bar_content=(Button)findViewById(R.id.title_bar_content);
        anim_zoom=(Button)findViewById(R.id.anim_zoom);
        anim_scale=(Button)findViewById(R.id.anim_scale);
        anim_slide=(Button)findViewById(R.id.anim_slide);

        attach.setOnClickListener(this);
        changing_fragments.setOnClickListener(this);
        left_and_right.setOnClickListener(this);
        responsive_ui.setOnClickListener(this);
        viewpager.setOnClickListener(this);
        title_bar_slide.setOnClickListener(this);
        title_bar_content.setOnClickListener(this);
        anim_zoom.setOnClickListener(this);
        anim_scale.setOnClickListener(this);
        anim_slide.setOnClickListener(this);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        Class<?> cls = null;
        switch (v.getId()){
            case R.id.attach:
                cls = AttachExample.class;
                break;
            case R.id.changing_fragments:
                cls = FragmentChangeActivity.class;
                break;
            case R.id.responsive_ui:
                cls = ResponsiveUIActivity.class;
                break;
            case R.id.viewpager:
                cls = ViewPagerActivity.class;
                break;
            case R.id.left_and_right:
                cls = LeftAndRightActivity.class;
                break;
            case R.id.title_bar_slide:
                cls = SlidingTitleBar.class;
                break;
            case R.id.title_bar_content:
                cls = SlidingContent.class;
                break;
            case R.id.anim_zoom:
                cls = CustomZoomAnimation.class;
                break;
            case R.id.anim_scale:
                cls = CustomScaleAnimation.class;
                break;
            case R.id.anim_slide:
                cls = CustomSlideAnimation.class;
                break;
        }
        Intent intent=new Intent(this,cls);
        startActivity(intent);
    }


    /**
     * 今日头条请求数据
     */
    private void jsonRequestGetHttpsExample() {
        final String url = "https://c.m.163.com/nc/article/headline/T1348647909107/0-20.html";
        VolleyDataRequester.withDefaultHttps(this)
                .setUrl(url)
                .setJsonResponseListener(new VolleyDataRequester.JsonResponseListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            //TODO 待解析字符串
                            String s = response.toString();
                            Gson gson = new Gson();

                            Map<String, List<NewsSummary>> map = gson.fromJson(s, new TypeToken<Map<String, List<NewsSummary>>>() {
                            }.getType());

                            Toast.makeText(MainActivity.this, "HTTPS/GET, JsonRequest successfully.", Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                })
                .setResponseErrorListener(new VolleyDataRequester.ResponseErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                })
                .requestJson();
    }

    /**
     * 考试测试数据
     */
    private void stringRequestGetHttpExample() {
        String url = "http://mock.eoapi.cn/success/cjTiF8H7R6Dw4fmRsuUS3H7ZPaVUJzRW";

        VolleyDataRequester.withHttp(this)
                .setUrl(url)
                .setMethod(VolleyDataRequester.Method.GET)
                .setStringResponseListener(new VolleyDataRequester.StringResponseListener() {
                    @Override
                    public void onResponse(String response) {
                        TestData data = null;
                        try {
                            //TODO 待处理字符串
                            data = new Gson().fromJson(new String(response.getBytes("ISO-8859-1"), "utf-8"), TestData.class);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                        Toast.makeText(MainActivity.this, "HTTP/POST,StringRequest successfully.", Toast.LENGTH_SHORT).show();
                    }
                })
                .requestString();
    }


}
